# coding=utf-8
# 全世界最帅的帅哥
from flask_restful import reqparse
from sqlalchemy.exc import OperationalError, SQLAlchemyError
from ysgg1_club_comment.modules.auth_user import AuthUser
from ysgg1_club_comment.modules import db
from flask import current_app, request
from ysgg1_club_auth.auth_serializers.user_serializer import UserSerializer
from ysgg1_club_comment.utils.fbmsg import FBMsg
from flask import current_app


def query_user_by_id(user_id):
    user = AuthUser.query.filter_by(id=user_id, is_delete=0).first()
    data = UserSerializer(user).to_dict()
    return data


def update_user(user_id):
    try:
        user = AuthUser.query.filter_by(id=user_id, is_delete=0).first()
        if not user:
            return FBMsg.error_user()
        parser = reqparse.RequestParser()
        parser.add_argument('email')
        parser.add_argument("password")
        parser.add_argument('nickName')
        parser.add_argument('sex')
        parser.add_argument('avatar')
        parser.add_argument('introduce')
        parser.add_argument('extra_json')

        args = parser.parse_args()
        email = args.email
        password = args.password
        nick_name = args.nickName
        sex = args.sex
        avatar = args.avatar
        introduce = args.introduce
        extra_json = args.extra_json

        user.email = email if email else user.email
        user.nick_name = nick_name if nick_name else user.nick_name
        user.introduce = introduce if introduce else user.introduce
        user.extra_json = extra_json if extra_json else user.extra_json
        user.avatar = avatar if avatar else user.avatar
        user.pwd = password if password else user.sex
        user.sex = sex if sex else user.sex
        db.session.add(user)
        db.session.commit()
        current_app.logger.info(f"修改用户id为{user.id}成功")
        return {"code": 200, "success": True, "message": "更新成功"}

    except (Exception, OperationalError, SQLAlchemyError) as e:
        current_app.logger.error(e.__str__())
        db.session.rollback()
        return FBMsg.put_error_message()


def delete_user(user_id):
    user = AuthUser.query.filter_by(id=user_id, is_delete=0).first()
    if not user:
        return FBMsg.error_user()
    user.is_delete = 1

    db.session.add(user)
    db.session.commit()
    current_app.logger.info(f"删除用户id为{user_id}成功")
    return {"code": 200, "success": True, "message": "删除成功"}


def enable_or_disable_user():
    user_id = request.json.get("user_id", 0)
    user = AuthUser.query.filter_by(id=user_id, is_delete=0).first()
    if not user:
        return FBMsg.error_user()
    user.status ^= 1

    db.session.add(user)
    db.session.commit()
    current_app.logger.info(f"改变用户id为{user_id}的状态成功")
    return {"code": 200, "success": True, "message": f"改变用户id为{user_id}的状态成功"}


def get_user_info():
    data = request.get_json()
    user_id = data.get("user_id", 0)
    open_id = data.get("open_id", None)

    if user_id:
        user = AuthUser.query.filter_by(id=user_id, is_delete=0).first()
        user_dict = UserSerializer(user).to_dict()
        return user_dict
    elif open_id:
        user = AuthUser.query.filter_by(user_name=open_id, is_delete=0).first()
        user_dict = UserSerializer(user).to_dict()
        return user_dict
    return FBMsg.error_param()